package com.reader.cms.service.impl.magazine;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.reader.cms.entity.common.ReaderContentClassifyRelation;
import com.reader.cms.entity.common.ReaderContentTagsRelation;
import com.reader.cms.entity.common.ReaderContentTxt;
import com.reader.cms.entity.magazine.ReaderMagazineContent;
import com.reader.cms.mapper.common.ReaderContentClassifyRelationMapper;
import com.reader.cms.mapper.common.ReaderContentTagsRelationMapper;
import com.reader.cms.mapper.common.ReaderContentTxtMapper;
import com.reader.cms.mapper.magazine.ReaderMagazineContentMapper;
import com.reader.cms.service.magazine.IReaderMagazineContentService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author gumenghao
 * @@description 服务实现类
 * @date 2018-10-24
 */
@Service
@Transactional
public class ReaderMagazineContentServiceImpl extends ServiceImpl<ReaderMagazineContentMapper, ReaderMagazineContent> implements IReaderMagazineContentService {


    @Autowired
    private ReaderMagazineContentMapper readerMagazineContentMapper;

    @Autowired
    private ReaderContentTxtMapper readerContentTxtMapper;

    @Autowired
    private ReaderContentTagsRelationMapper readerContentTagsRelationMapper;

    @Autowired
    private ReaderContentClassifyRelationMapper readerContentClassifyRelationMapper;

    /**
     * @param currentPage 当前页
     * @param size        每页显示条数
     * @param magazineId  杂志Id
     * @param magazineId  杂志Id
     * @return
     * @function 多表关联查询
     */
    @Transactional(readOnly = true)
    @Override
    public Page<ReaderMagazineContent> selectListPage(int currentPage, int size, String magazineId, String title) {
        Page<ReaderMagazineContent> page = new Page<>(currentPage, size);// 当前页，总条数 构造 page 对象
        return page.setRecords(readerMagazineContentMapper.selectListPage(page, magazineId,title));
    }

    //为单期杂志添加一篇新内容
    @Override
    public boolean addMagazineContent(ReaderMagazineContent readerMagazineContent) {
        readerMagazineContent.setState("Publish");
        boolean resultBoolean = this.insert(readerMagazineContent);

        //添加大文本关联表
        ReaderContentTxt readerContentTxt = new ReaderContentTxt();
        readerContentTxt.setDocId(readerMagazineContent.getId());
        readerContentTxt.setContentTxt(readerMagazineContent.getContent());
        Integer contentInsert = readerContentTxtMapper.insert(readerContentTxt);

        if (contentInsert == 1) {
            if (readerMagazineContent.getTagIds().size() != 0) {
                //大文本添加成功后，继续添加文章内容和标签关联表
                insertContentTags(readerMagazineContent);
            }
            if(readerMagazineContent.getContentClassifies().size() != 0){
                //大文本添加成功后，继续添加文章内容和分类关联表
                insertContentClassifies(readerMagazineContent);
            }

        }
        return resultBoolean;
    }


    //根据文章内容id　查询并回显此内容
    @Override
    public ReaderMagazineContent selectSingleContent(String id) {
        ReaderMagazineContent readerMagazineContent = readerMagazineContentMapper.selectSingleContent(id);
        if(readerMagazineContent != null){
            //内容标签更新
            EntityWrapper<ReaderContentTagsRelation> wrapper = new EntityWrapper<>();
            wrapper.eq("doc_id", readerMagazineContent.getId());
            List<ReaderContentTagsRelation> tagsRelations = readerContentTagsRelationMapper.selectList(wrapper);
            List<Integer> tagIds = new ArrayList<>();
            if (tagsRelations!= null && tagsRelations.size() != 0) {
                tagsRelations.forEach((tagsRelation) -> {
                    tagIds.add(tagsRelation.getTagId());
                });
            }
            readerMagazineContent.setTagIds(tagIds);

            //内容分类更新
            EntityWrapper<ReaderContentClassifyRelation> classifyRelationEntityWrapper = new EntityWrapper<>();
            classifyRelationEntityWrapper.eq("doc_id", readerMagazineContent.getId());
            List<ReaderContentClassifyRelation> classifyRelations = readerContentClassifyRelationMapper.selectList(classifyRelationEntityWrapper);
            List<Integer> classifyIds = new ArrayList<>();
            if (classifyRelations!= null && classifyRelations.size() != 0) {
                classifyRelations.forEach((classifyRelation) -> {
                    classifyIds.add(classifyRelation.getClassifyId());
                });
            }
            readerMagazineContent.setContentClassifies(classifyIds);
        }
        return readerMagazineContent;
    }

    //修改杂志单篇文章信息
    @Override
    public boolean updateMagazineContent(ReaderMagazineContent readerMagazineContent) {
        boolean resultBoolean = this.updateById(readerMagazineContent);
        //文章内容修改成功后进行大文本表的修改
        ReaderContentTxt readerContentTxt = new ReaderContentTxt();
        readerContentTxt.setContentTxt(readerMagazineContent.getContent());
        EntityWrapper<ReaderContentTxt> contentTxtEntityWrapper = new EntityWrapper<>();
        contentTxtEntityWrapper.where("doc_id = {0}",readerMagazineContent.getId());
        Integer updateTxt = readerContentTxtMapper.update(readerContentTxt,contentTxtEntityWrapper);
        if (updateTxt == 1) {

            //内容标签更新
            EntityWrapper<ReaderContentTagsRelation> tagsRelationEntityWrapper = new EntityWrapper<>();
            tagsRelationEntityWrapper.where("doc_id = {0}", readerMagazineContent.getId());
            readerContentTagsRelationMapper.delete(tagsRelationEntityWrapper);
            if (readerMagazineContent.getTagIds() !=null && readerMagazineContent.getTagIds().size() != 0) {
                insertContentTags(readerMagazineContent);
            }

            //内容分类更新
            EntityWrapper<ReaderContentClassifyRelation> classifyRelationEntityWrapper = new EntityWrapper<>();
            classifyRelationEntityWrapper.where("doc_id = {0}",readerMagazineContent.getId());
            readerContentClassifyRelationMapper.delete(classifyRelationEntityWrapper);
            if(readerMagazineContent.getContentClassifies() != null && readerMagazineContent.getContentClassifies().size() != 0){
                insertContentClassifies(readerMagazineContent);
            }

        }
        return resultBoolean;
    }

    //添加文章内容和分类关联表
    private void insertContentClassifies(ReaderMagazineContent readerMagazineContent) {
        List<ReaderContentClassifyRelation> relations = new ArrayList<>();
        List<Integer> contentClassifies = readerMagazineContent.getContentClassifies();
        contentClassifies.forEach((contentClassifyId) -> {
            ReaderContentClassifyRelation relation = new ReaderContentClassifyRelation();
            relation.setClassifyId(contentClassifyId);
            relation.setDocId(readerMagazineContent.getId());
            relation.setInsertDt(new Date());
            relation.setInsertUserId(readerMagazineContent.getInsertUserId());
            relation.setUpdtUserId(readerMagazineContent.getUpdtUserId());
            relations.add(relation);
        });
        readerContentClassifyRelationMapper.insertBaths(relations);

    }


    //添加文章内容和标签关联表
    private void insertContentTags(ReaderMagazineContent readerMagazineContent) {
        List<ReaderContentTagsRelation> relations = new ArrayList<>();
        List<Integer> tagIds = readerMagazineContent.getTagIds();
        tagIds.forEach((tagId) -> {
            ReaderContentTagsRelation relation = new ReaderContentTagsRelation();
            relation.setDocId(readerMagazineContent.getId());
            relation.setTagId(tagId);
            relation.setInsertDt(new Date());
            relation.setInsertUserId(readerMagazineContent.getInsertUserId());
            relation.setUpdtUserId(readerMagazineContent.getUpdtUserId());
            relations.add(relation);
        });
        readerContentTagsRelationMapper.insertBaths(relations);

    }

}